package com.iims.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.core.domain.model.LoginUser;
import com.iims.common.utils.SecurityUtils;
import com.iims.common.utils.SeqNumUtils;
import com.iims.market.domain.CmisOrder;
import com.iims.market.mapper.OrderMapper;
import com.iims.market.service.IOrderService;
import com.iims.oder.vo.request.GroupOrderSearchQuery;
import com.iims.oder.OrderUtils;
import com.iims.oder.vo.resp.OrderDetailVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/7/3 13:32
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, CmisOrder> implements IOrderService {


    @Override
    public List<OrderDetailVo> selectCmisOrderList(CmisOrder cmisOrder) {
        List list = baseMapper.selectOrderSimpleList(cmisOrder);
        return SeqNumUtils.initSeqNum(cmisOrder, list);
    }

    @Override
    public List<OrderDetailVo> selectCmisOrderListByGroup(GroupOrderSearchQuery groupSearchQuery) {
        groupSearchQuery.generateSqlArgs();
        List list = baseMapper.selectOrderSimpleListByGroup(groupSearchQuery);
        return SeqNumUtils.initSeqNum(groupSearchQuery, list);
    }

    @Override
    public List<OrderDetailVo> selectProEnableCmisOrderList(Long proId) {
        return baseMapper.selectProEnableCmisOrderList(proId);
    }

    @Override
    public OrderDetailVo selectDetailById(Long orderId) {
        return baseMapper.selectOrderSimpletVoById(orderId);
    }


    @Override
    public int insertCmisOrder(CmisOrder cmisOrder) {
        //检查是否产品呢重复下单
/*        if (orderProductMapper.selectContractOrderProductCount
                (cmisOrder.getContractId(),
                        cmisOrder.getProducts()) > 0) {
            throw new CustomException("此合同已添加了你选择的产品，请检查");
        }*/
        LoginUser loginUser = SecurityUtils.getLoginUser();
        cmisOrder.setOrderCode(OrderUtils.getOrderCode(loginUser.getUser().getUserId()));
        baseMapper.insert(cmisOrder);
        return 1;
    }

    @Override
    public int updateCmisOrder(CmisOrder cmisOrder) {
/*        //检查是否产品呢重复下单
        if (orderProductMapper.selectContractOrderProductCount
                (cmisOrder.getContractId(),
                        cmisOrder.getProducts()) > 0) {
            throw new CustomException("此合同已添加了你选择的产品，请检查");
        }*/

        LambdaQueryWrapper<CmisOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(CmisOrder::getOrderCode)
                .eq(CmisOrder::getOrderId, cmisOrder.getOrderId());
        CmisOrder OLD = baseMapper.selectOne(queryWrapper);
        cmisOrder.setOrderCode(OLD.getOrderCode());
        baseMapper.updateById(cmisOrder);
        return 1;
    }

    @Override
    public int deleteCmisOrderById(Long orderId) {
        baseMapper.deleteById(orderId);
        return 1;
    }

    @Override
    public boolean checkCmisOrderUse(Long orderId) {
        return baseMapper.checkCmisOrderUse(orderId) > 0;
    }
}
